<template>
  <div>
    <header>
      <n-bar></n-bar>
      <div class="header_box">
        <!-- 推荐 -->
        <div class="recommend">
          <ul>
            <li>
              <a href="javascript:;">全部(20)</a>
            </li>
            <li>
              <a href="javascript:;">降价(4)</a>
            </li>
            <li>
              <a href="javascript:;">常卖(2)</a>
            </li>
            <li>
              <a href="javascript:;">分类</a>
              <span class="iconfont icon-jiage"></span>
            </li>
          </ul>
        </div>
      </div>
    </header>
    <!-- 小u自营 -->
    <div class="wrap">
      <div class="ziying">
        <div class="left">
          <span>小U自营</span>
        </div>
        <div class="right">
          <span>已免运费</span>
          <span>领券</span>
        </div>
      </div>
    </div>
    <div v-if="cartlist.length > 0" class="cart">
      <div v-for="item in cartlist" :key="item.id" class="cartinfo">
        <div class="checkinfo">
          <van-checkbox
            v-model="item.checked"
            checked-color="#ee0a24"
            @click="checkone"
          ></van-checkbox>
        </div>
        <van-swipe-cell class="checkinfo_r">
          <van-card
            :num="item.num"
            :price="item.price | toPrice"
            :title="item.goodsname"
            class="goods-card"
            :thumb="item.img"
          >
            <template #footer>
              <van-stepper
                @change="changeNum(item.id)"
                v-model="item.num"
                disable-input
              />
            </template>
          </van-card>

          <template #right>
            <van-button
              @click="del(item.id)"
              square
              text="删除"
              type="danger"
              class="delete-button"
            />
          </template>
        </van-swipe-cell>
      </div>
    </div>

    <div v-else>
      <van-empty description="购物城空空如也，快去买买mai~~~" />
    </div>
    <van-submit-bar @submit="goOrder" :price="allPrice" button-text="提交订单">
      <van-checkbox @click="allCheck" v-model="checkall" checked-color="#ee0a24"
        >全选</van-checkbox
      >
    </van-submit-bar>
  </div>
</template>

<script>
import { cartEdit, cartDel, cartList, orderAdd } from "../requset/app";
import { mapGetters } from "vuex";
import { Toast } from "vant";
export default {
  data() {
    return {
      cartlist: [], //购物车列表
      checkall: false,
    };
  },
  computed: {
    ...mapGetters(["getUserInfo"]),
    allPrice() {
      let sum = 0;
      this.cartlist.map((item) => {
        if (item.checked) {
          sum += item.price * item.num;
        }
      });
      return sum * 100;
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 全选空单选
    allCheck() {
      this.cartlist.map((item) => {
        item.checked = this.checkall;
      });
    },
    // 单选事件
    checkone() {
      this.checkall = this.cartlist.every((item) => item.checked);
    },
    getList() {
      cartList({
        uid: this.getUserInfo.uid,
      }).then((res) => {
        // console.log(res, "响应");
        if (res.code == 200) {
          this.cartlist = res.list ? res.list : [];
          this.cartlist.map((item) => {
            this.$set(item, "checked", false);
          });
        }
      });
    },

    // 购物车++--
    changeNum(id) {
      // console.log(n, "111111");
      cartEdit({
        id,
        type: 1,
      }).then((res) => {
        // console.log(res, "修改购物车");
        if (res.code == 200) {
          // alert(res.data.msg);
        }
      });
    },

    // 购物车删除
    del(id) {
      cartDel({ id }).then((res) => {
        console.log(res, "购物车删除");
        if (res.code == 200) {
          Toast(res.msg);
          this.getList();
        }
      });
    },
    // 点击跳转到订单
    goOrder() {
      if (this.cartlist.some((item) => item.checked)) {
        let num = 0;
        let val = "";
        let arr = [];
        this.cartlist.map((item) => {
          if (item.checked) {
            num += item.num;
            val += `,${item.id}`;
            arr.push(item);
          }
        });
        let orderInfo = {
          countmoney: this.allPrice,
          countnumber: num,
          idstr: val.slice(1),
        };
        sessionStorage.setItem("orderInfo", JSON.stringify(orderInfo));
        sessionStorage.setItem("arr", JSON.stringify(arr));
        this.$router.push("/order");
      } else {
        Toast.fail("至少选择一件商品去结算~~~~~~~~~");
      }
    },
  },
};
</script>

<style scoped>
.cart{
  min-height: 200vh;
}
.cartinfo {
  width: 100%;
  display: flex;
  /* background-color: pink; */
}
.checkinfo {
  margin-top: 0.5rem;
  margin-left: 0.15rem;
}
.checkinfo_r {
  flex: 1;
}
.van-submit-bar {
  bottom: 49px;
}
#shopcar {
  width: 3.75rem;
  margin: 0 auto;
  background-color: #f2f2f2;
  min-height: 200vh;
}

/* 头部 */
header {
  background-color: #ff6040;
  box-shadow: 0 2px 6px 0 rgba(255, 149, 128, 0.5);
  box-sizing: border-box;
}

/* 顶部信息 */
header .top_box {
  height: 0.44rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.06rem 0 0.17rem;
  box-sizing: border-box;
}

header .top_box h2 {
  opacity: 0.8;
  font-size: 0.17rem;
  color: #ffffff;
  margin-left: 0.23rem;
}

header .top_box .more {
  width: 0.87rem;
  height: 0.32rem;
  background: rgba(255, 119, 51, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 0.405rem;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
}

header .top_box .more .line {
  width: 1px;
  height: 0.19rem;
  background: rgba(255, 255, 255, 0.25);
}

header .top_box .more a span {
  font-size: 0.19rem;
  opacity: 0.8;
  color: #ffffff;
}

/* 推荐 */
.header_box .recommend ul {
  height: 0.32rem;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

.header_box .recommend ul li a {
  font-size: 0.14rem;
  color: #fff;
}

.header_box .recommend ul li:nth-child(1) a {
  font-size: 0.16rem;
  font-weight: bold;
}

.header_box .recommend ul li:nth-child(1) {
  position: relative;
}

.header_box .recommend ul li:nth-child(1) a::after {
  content: "";
  width: 0.2rem;
  height: 0.02rem;
  background: #ffffff;
  border-radius: 0.01rem;
  position: absolute;
  bottom: -0.04rem;
  left: 0.2rem;
}

.recommend ul li span {
  font-size: 0.13rem;
  color: #fff;
}
.tui-checkbox:checked {
  border-color: #ff6040;
}
.tui-checkbox {
  width: 0.2rem;
  height: 0.2rem;
  background: #f2f2f2;
  margin: 6px;
  border: solid 1px #999999;
  outline: none;
  -webkit-border-radius: 50%;
  border-radius: 50%;
  position: relative;
  -webkit-appearance: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-transition: background-color ease 0.1s;
  transition: background-color ease 0.1s;
}
.tui-checkbox:checked::after {
  content: "";
  top: 50%;
  left: 50%;
  margin-top: -6px;
  margin-left: -6px;
  position: absolute;
  background: #ff6040;
  border-top: none;
  border-right: none;
  height: 12px;
  width: 12px;
  border-radius: 50%;
}

/* 小u自营 */
.ziying {
  width: 3.75rem;
  height: 0.36rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.1rem 0 0.11rem;
  box-sizing: border-box;
  margin-bottom: 0.1rem;
  background-color: #fff;
}

.ziying .left {
  font-size: 0.16rem;
  color: #333333;
  display: flex;
  align-content: center;
}
.ziying .left > span {
  margin-top: 4px;
}
.ziying .right span:nth-child(1) {
  font-size: 0.12rem;
  color: #333333;
  margin-right: 0.1rem;
}

.ziying .right span:nth-child(2) {
  font-size: 0.12rem;
  color: #ff6040;
  border-left: 1px solid #979797;
  padding-left: 0.1rem;
  box-sizing: border-box;
}

.shop_info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.15rem;
}
.ziying_bot .left {
  width: 1.44rem;
  margin-right: 0.15rem;
}
.delete-button {
  height: 100%;
}
.ziying_bot .left img {
  width: 1.08rem;
  height: 1.08rem;
}

.ziying_bot .left span {
  font-size: 0.14rem;
  color: #ff6040;
  padding: 0 0.07rem;
  box-sizing: border-box;
}

.ziying_bot .right {
  width: 2.16rem;
}

.ziying_bot .right p:nth-child(1) {
  font-size: 0.16rem;
  color: #333333;
}

.ziying_bot .right p:nth-child(2) {
  font-size: 0.14rem;
  color: #999999;
}

.ziying_bot .right p:nth-child(3) select {
  width: 0.8rem;
  height: 0.16rem;
  font-size: 0.1rem;
  color: #333333;
  background: #f2f2f2;
  border-radius: 1px;
  margin-top: 0.07rem;
}

.ziying_bot .right p:nth-child(4) {
  font-size: 0.1rem;
  color: #ff6040;
}

.ziying_bot .right .box {
  display: flex;
  justify-content: space-between;
  height: 0.16rem;
  line-height: 0.16rem;
  margin-top: 0.04rem;
}

.ziying_bot .right .box .price {
  width: 0.82rem;
  display: flex;
}

.ziying_bot .right .box .price span:nth-of-type(1) {
  font-size: 0.1rem;
  color: #ff6040;
}

.ziying_bot .right .box .price span:nth-of-type(2) {
  font-size: 0.2rem;
  color: #ff6040;
}

.ziying_bot .right .box .price del {
  font-size: 0.1rem;
  color: #999999;
}

.ziying_bot .right .box .amount {
  display: flex;
  align-items: center;
  padding-right: 0.1rem;
  box-sizing: border-box;
}

.ziying_bot .right .box .amount span:nth-child(1) {
  font-size: 0.14rem;
  color: #333;
}

.ziying_bot .right .box .amount span:nth-child(2) {
  width: 0.4rem;
  height: 0.2rem;
  background: #f2f2f2;
  font-size: 0.14rem;
  color: #333333;
  border-radius: 0.01rem;
  text-align: center;
  line-height: 0.2rem;
  margin: 0 0.05rem;
}

.ziying_bot .right .box .amount span:nth-child(3) {
  font-size: 0.14rem;
  color: #333;
}
.shop {
  font-size: 20px;
  margin-left: 20px;
}
</style>
